//
//  WXKeyBoardView.swift
//  weibo
//
//  Created by 王旭 on 16/4/27.
//  Copyright © 2016年 王旭. All rights reserved.


//自定义键盘视图

import UIKit

class WXKeyBoardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
//        backgroundColor = UIColor.grayColor()
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(emotionView)
        addSubview(emotionCollectionView)
        addSubview(pageControl)
        
        emotionView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(35)
        }
        emotionCollectionView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emotionView.snp_top)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(emotionView.snp_top)
            make.centerX.equalTo(self)
        }
        
        //初始化按钮,pageControl,表情键盘的状态
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            //1.获得indexPath
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            //2.滚动collectionView
            self.emotionCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            //3.设置pageControl
            self.pageControlWithScroll(indexPath)
            //4.设置按钮
            self.emotionView.selectedSection = indexPath.section
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = emotionCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        //设置大小
        layout.itemSize = CGSize(width: SCREEN_W, height: bounds.size.height - emotionView.bounds.size.height)
    }
    
//懒加载collectionView
    private lazy var emotionCollectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let emotionCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        //代理
        emotionCollectionView.dataSource = self
        emotionCollectionView.delegate = self
        //注册cell
        emotionCollectionView.registerClass(WXEmotionItemCell.self, forCellWithReuseIdentifier: "emotion_cell")
        //设置背景色
        emotionCollectionView.backgroundColor = UIColor.clearColor()
        //layout设置
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //collectionView设置
        emotionCollectionView.bounces = false
        emotionCollectionView.showsHorizontalScrollIndicator = false
        emotionCollectionView.pagingEnabled = true
        
        return emotionCollectionView
    }()
    
//懒加载toolbar
    //1.stackview->闭包 2.闭包 3.view->stackView
    private lazy var emotionView : WXEmoticonView = {
        let emotionView = WXEmoticonView(frame: CGRectZero)
        emotionView.tag = 900
        emotionView.emotionToolBarClickClosure = {(type:EmoticonButtonType) in
            print(type)
            //获得indexPath,切换item
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self.emotionCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
//            print(indexPath)
            
            //设置pageControl的状态
            self.pageControlWithScroll(indexPath)

        }
        return emotionView
    }()
    
//懒加载分页控件
    private lazy var pageControl : UIPageControl = {
       let pageControl = UIPageControl()
        pageControl.hidden = true
        //--------------------以下为测试代码-----------------
//                pageControl.currentPageIndicatorTintColor = UIColor.redColor()
//                pageControl.pageIndicatorTintColor = UIColor.blackColor()
//        pageControl.currentPageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_selected")!)
//        pageControl.pageIndicatorTintColor = UIColor(patternImage: UIImage(named: "compose_keyboard_dot_normal")!)
        //--------------------以上为测试代码-----------------
        //进行时设置
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        return pageControl
    }()
}

//数据源方法
extension WXKeyBoardView : UICollectionViewDataSource,UICollectionViewDelegate {
    //多少组-三级数组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
       return WXEmotionViewModel.sharedEmotionViewModel.allArr.count
    }
    //多少行-二级数组
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WXEmotionViewModel.sharedEmotionViewModel.allArr[section].count
    }
    //每行内容-一级数组
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("emotion_cell", forIndexPath: indexPath) as! WXEmotionItemCell
        //--------------------以下为测试代码-----------------
//        cell.backgroundColor = RandomColor
        //--------------------以上为测试代码-----------------
        //传递数据
        let emotions = WXEmotionViewModel.sharedEmotionViewModel.allArr[indexPath.section][indexPath.item]
        cell.emotions = emotions
        cell.indexPath = indexPath
        return cell
    }
    
    //监听collectionView的滚动
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //1.计算中心点
        var center = scrollView.center
        center.x = center.x + scrollView.contentOffset.x
        //2.获得屏幕的可见的cell
        let cells = emotionCollectionView.visibleCells()
        //3.判断点是否位于cell上
        let indexPath :NSIndexPath //只能赋值一次
        if cells.count == 2{
            let left = cells.first!
            let right = cells.last!
            if CGRectContainsPoint(left.frame, center) {
                indexPath = emotionCollectionView.indexPathForCell(left)!
            }else{
                indexPath = emotionCollectionView.indexPathForCell(right)!
            }
        }else{
            indexPath = emotionCollectionView.indexPathForCell(cells.first!)!
        }
        //4.给toolbar传递选中的section
        emotionView.selectedSection = indexPath.section
        //5.设置pageControl的状态
        pageControlWithScroll(indexPath)
    }
    
    //设置pageControl的状态
    func pageControlWithScroll(indexPath:NSIndexPath) {
        //数量
        pageControl.numberOfPages = WXEmotionViewModel.sharedEmotionViewModel.allArr[indexPath.section].count
        //当前
        pageControl.currentPage = indexPath.item
        //显示状态
        pageControl.hidden = indexPath.section == 0
    }
}
